package com.pds.web.maintenance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.maintenance.entity.PdsEnvironmentGuidance;
import com.pds.web.maintenance.entity.PdsEnvironmentGuidanceStep;
import com.pds.web.maintenance.mapper.PdsEnvironmentGuidanceMapper;
import com.pds.web.maintenance.mapper.PdsEnvironmentGuidanceStepMapper;
import com.pds.web.maintenance.service.PdsEnvironmentGuidanceService;
import com.pds.web.maintenance.vo.EnvironmentGuidanceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 监管部门规范指导表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-02-02
 */
@Service("pdsEnvironmentGuidance")
public class PdsEnvironmentGuidanceServiceImpl extends ServiceImpl<PdsEnvironmentGuidanceMapper, PdsEnvironmentGuidance> implements PdsEnvironmentGuidanceService {
    @Resource
    private PdsEnvironmentGuidanceMapper pdsEnvironmentGuidanceMapper;
    @Resource
    private PdsEnvironmentGuidanceStepMapper pdsEnvironmentGuidanceStepMapper;

    @Override
    public List<EnvironmentGuidanceVO> selectList(PdsEnvironmentGuidance pdsEnvironmentGuidance) {
        List<EnvironmentGuidanceVO> list = new ArrayList<>();
        QueryWrapper<PdsEnvironmentGuidance> wrapper = new QueryWrapper<>();
        String guideTitle = pdsEnvironmentGuidance.getGuideTitle();
        String guideContent = pdsEnvironmentGuidance.getGuideContent();
        if(!Objects.isNull(guideTitle) && !guideTitle.isEmpty()){
            wrapper.like("guide_title",guideTitle);
        }
        if(!Objects.isNull(guideContent) && !guideContent.isEmpty()){
            wrapper.like("guide_content",guideContent);
        }
        List<PdsEnvironmentGuidance> guidanceList = pdsEnvironmentGuidanceMapper.selectList(wrapper);
        if(!Objects.isNull(guidanceList) && !guidanceList.isEmpty()){
            for (PdsEnvironmentGuidance guidance : guidanceList) {
                Long id = guidance.getId();
                EnvironmentGuidanceVO vo = new EnvironmentGuidanceVO();
                BeanUtils.copyProperties(guidance,vo);
                QueryWrapper<PdsEnvironmentGuidanceStep> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("guidance_id",id);
                queryWrapper.orderByAsc("step_order");
                List<PdsEnvironmentGuidanceStep> steps = pdsEnvironmentGuidanceStepMapper.selectList(queryWrapper);
                vo.setSteps(steps);
                list.add(vo);
            }
        }
        return list;
    }

    @Override
    public PdsEnvironmentGuidance queryById(Long id) {
        return pdsEnvironmentGuidanceMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public PdsEnvironmentGuidance insert(PdsEnvironmentGuidance pdsEnvironmentGuidance) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsEnvironmentGuidance.setUserId(userId);
        pdsEnvironmentGuidance.setCreateBy(nickName);*/
        pdsEnvironmentGuidance.setCreateTime(date);
        pdsEnvironmentGuidance.setStatus("1");
        pdsEnvironmentGuidanceMapper.insert(pdsEnvironmentGuidance);
        return pdsEnvironmentGuidance;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean update(PdsEnvironmentGuidance pdsEnvironmentGuidance) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsEnvironmentGuidance.setUpdateBy(nickName);*/
        pdsEnvironmentGuidance.setUpdateTime(date);
        return pdsEnvironmentGuidanceMapper.updateById(pdsEnvironmentGuidance) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return pdsEnvironmentGuidanceMapper.deleteById(id) > 0;
    }
}